﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Newtonsoft.Json;
using WMS.FC.Domain.PersistenceObject.System;
using WMS.FC.Infrastructure.Common;

namespace WMS.FC.Infrastructure.POConfig.System
{
    public class DictionaryConfig : POBaseConfig<DictionaryPO>
    {
        public override void InitializeData(EntityTypeBuilder<DictionaryPO> builder)
        {
            var fileList = Directory.GetFiles("Config");
            var allDictionaryList = new List<DictionaryPO>();
            foreach (var file in fileList)
            {
                if (file.StartsWith(@"Config\Dic") && file.EndsWith("json"))
                {
                    var dictionaryList = JsonConvert.DeserializeObject<List<DictionaryPO>>(File.ReadAllText(file));
                    if (dictionaryList != null)
                    {
                        allDictionaryList.AddRange(dictionaryList);
                    }
                }
            }

            builder.HasData(new DictionaryPO()
            {
                Id = SnowflakeIdHelper.Instance.NextId(),
                Code = "0",
                Name = "字典类型",
                TypeCode = "0",
                SortNumber = 0,
                CreateDate = DateTime.Now,
                CreatedBy = "系统自动生成",
                ModifyDate = null,
                ModifiedBy = null,
                ModifyIp = null,
                IsDeleted = false,
            });

            foreach (var dictionary in allDictionaryList)
            {
                builder.HasData(new DictionaryPO()
                {
                    Id = SnowflakeIdHelper.Instance.NextId(),
                    Code = dictionary.Code,
                    Name = dictionary.Name,
                    TypeCode = dictionary.TypeCode,
                    SortNumber = dictionary.SortNumber,
                    CreateDate = DateTime.Now,
                    CreatedBy = "系统自动生成",
                    ModifyDate = null,
                    ModifiedBy = null,
                    ModifyIp = null,
                    IsDeleted = false,
                });
            }
        }

        public override void MapColumn(EntityTypeBuilder<DictionaryPO> builder, ref int index)
        {
            builder.HasComment("数据字典表");
            builder.Property(entity => entity.Code).ConfigColumn("CODE", "编码", 50, true).HasColumnOrder(index++);
            builder.Property(entity => entity.Name).ConfigColumn("NAME", "名称", 100, true).HasColumnOrder(index++);
            builder.Property(entity => entity.TypeCode).ConfigColumn("TYPE_CODE", "类型编码", 50, true).HasColumnOrder(index++);
            builder.Property(entity => entity.SortNumber).ConfigColumn("SORT_NUMBER", "排序").HasColumnOrder(index++);
        }

        public override void MapIndex(EntityTypeBuilder<DictionaryPO> builder)
        {
            builder.HasIndex(entity => entity.Code).HasDatabaseName($"UN_{TableName}_1").IsUnique();
            builder.HasIndex(entity => entity.TypeCode).HasDatabaseName($"IDX_{TableName}_1");
        }
    }
}
